package org.wk.news.api.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.wk.core.common.view.ResponseVo;
import org.wk.news.service.NewsManageService;
import org.wk.news.service.NewsPraiseService;
import org.wk.news.view.NewsManageVo;

/**
 * @className NewsController
 * @description 资讯
 * @author ZhaoPing'an
 * @date 2018/6/8 15:45
 */
@Controller
public class NewsController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private NewsManageService manageService;
    @Autowired
    private NewsPraiseService newsPraiseService;

    /**
     * @Description 获取资讯列表
     *  分页显示
     * @Param []
     * @param page_num 页数
     * @param page_size 页容量
     * @author ZhaoPing'an
     * @Date 2018/6/9 12:18
     */
    @RequestMapping(value="/news",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResponseVo findNews(@RequestParam(required = false,defaultValue = "1") Integer page_num, @RequestParam(required = false,defaultValue = "15") Integer page_size){
        logger.info("资讯列表>>>>{}");
        ResponseVo result = manageService.findNews(page_num,page_size);
        logger.info("资讯列表结果>>>>{}",result);
        return result;
    }

    /**
     * @Description 获取新闻详情
     * @Param [newsId] 新闻ID
     * @author ZhaoPing'an
     * @Date 2018/6/9 13:56
     */
    @RequestMapping(value="/news/{news_id}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String findNewsInfo(@PathVariable("news_id") String newsId, @RequestParam(value = "userId", required = false) String userId ,ModelMap modelMap){
        logger.info("获取新闻资讯详细信息>>>>{}");
        NewsManageVo newsManageVo = manageService.findNewsInfo(newsId);

        modelMap.put("news", newsManageVo);
        modelMap.put("userId", userId);
        if (userId==null) {
            modelMap.put("praiseStatus",false);
        } else {
            modelMap.put("praiseStatus",newsPraiseService.selectPraise(userId,newsId));
        }
        logger.info("获取新闻资讯详细信息>>>>{}",JSONObject.toJSONString(newsManageVo));
        return "news/detail";
    }

    /**
     * @description 获取资讯分享详情
     * @param
     * @author ZhaoPing'an
     * @date 2018/6/9 14:03
     */
    @ResponseBody
    @RequestMapping(value="/news/{news_id}/share_info",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVo findNewsShareInfo(@PathVariable("news_id") String newsId){
        logger.info("获取新闻资讯分享信息>>>>{}");
        ResponseVo responseVo = manageService.findShareInfoByNewsId(newsId);
        logger.info("获取新闻资讯分享信息>>>>{}",responseVo);
        return responseVo;
    }
}
